package game.view;

import game.Board;
import game.UpdateType;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * Board panel.
 * @author Natchanon Hongladaromp
 *
 */
public class BoardUI extends JPanel implements Observer
{
	private JTextField num1;
	private JTextField num2;
	private JTextField operation;
	private Board board;

	/**
	 * Construct for this panel.
	 * @param board
	 */
	public BoardUI(Board board)
	{
		this.board = board;
		num1 = new JTextField(20);
		num2 = new JTextField(20);
		operation = new JTextField(20);
		initComponents();
	}

	/**
	 * initial components.
	 */
	private void initComponents()
	{
		Font f = new Font(null, Font.PLAIN, 24);
		num1.setFont(f);
		num1.setHorizontalAlignment(SwingConstants.CENTER);
		num2.setFont(f);
		num2.setHorizontalAlignment(SwingConstants.CENTER);
		operation.setFont(f);
		operation.setHorizontalAlignment(SwingConstants.CENTER);
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		num1.setEditable(false);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.insets = new Insets(10,5,10,5);
		c.ipadx = 40;
		c.ipady = 20;
		c.gridx = 0;
		c.gridy = 0;
		add(num1,c);
		
		operation.setEditable(false);
		c.gridx = 1;
		add(operation,c);
		
		num2.setEditable(false);
		c.gridx = 2;
		add(num2,c);
	}

	@Override
	public void update(Observable o, Object arg)
	{
		if(arg == UpdateType.UPDATE_BOARD)
		{
			String[] texts = board.getBoard();
			num1.setText(texts[0]);
			operation.setText(texts[1]);
			num2.setText(texts[2]);
		}
	}
}
